package com.quick.system.manager;

import com.quick.common.constants.Constants;
import com.quick.common.utils.ServletUtils;
import com.quick.common.utils.StringUtils;
import com.quick.common.utils.ip.AddressUtils;
import com.quick.common.utils.ip.IpUtils;
import com.quick.common.utils.spring.SpringUtils;
import com.quick.system.domain.SysLoginInfo;
import com.quick.system.domain.SysOperateLog;
import com.quick.system.service.SysLoginInfoService;
import com.quick.system.service.SysOperateLogService;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

/**
 * 异步记录日志
 *
 * @Author: Administrator
 * @Date: 2023/7/17
 */
public class AsyncLogService {
    private static final Logger logger = LoggerFactory.getLogger("sys-user");

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public static TimerTask recordLoginInfo(final String username, final String status, final String message,
                                            final Object... args) {
        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIp(ip);
                String s = getBlock(ip) +
                        address +
                        getBlock(username) +
                        getBlock(status) +
                        getBlock(message);
                // 打印信息到日志
                logger.info(s, args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLoginInfo loginInfo = new SysLoginInfo();
                loginInfo.setUserName(username);
                loginInfo.setIpaddr(ip);
                loginInfo.setLoginLocation(address);
                loginInfo.setBrowser(browser);
                loginInfo.setOs(os);
                loginInfo.setMsg(message);
                // 日志状态
                if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
                    loginInfo.setStatus(Constants.SUCCESS);
                } else if (Constants.LOGIN_FAIL.equals(status)) {
                    loginInfo.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtils.getBean(SysLoginInfoService.class).insertSysLoginInfo(loginInfo);
            }
        };
    }

    /**
     * 格式化内容
     *
     * @param msg 内容
     * @return 格式化后的内容
     */
    public static String getBlock(Object msg) {
        if (msg == null) {
            msg = "";
        }
        return "[" + msg + "]";
    }

    /**
     * 操作日志记录
     *
     * @param operateLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOperateInfo(final SysOperateLog operateLog) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                operateLog.setOperateLocation(AddressUtils.getRealAddressByIp(operateLog.getOperateIp()));
                SpringUtils.getBean(SysOperateLogService.class).insertSysOperateLog(operateLog);
            }
        };
    }
}
